package www.dianbaozu.com.ui.login

import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import com.netease.nim.uikit.xpopup.CreateXPopupHelper
import com.netease.nim.uikit.xpopup.PopupViewEntity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.FlowPreview
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentBindPhoneBinding
import www.dianbaozu.com.ui.ProxyViewModel
import www.wen.com.common.lib.base.fragment.BaseReceiveSMSFragment
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.extension.getString
import www.wen.com.common.lib.extension.resetStartDestination
import www.wen.com.common.lib.extension.trimProxy
import www.wen.com.common.lib.helper.loading.KeyHelper
import www.wen.com.common.lib.helper.loading.KeyHelper.FORGET_REMAINDER_TIME
import www.wen.com.common.lib.helper.loading.KeyHelper.FORGET_SYSTEM_REMAINDER_TIME
import www.wen.com.common.lib.helper.loading.KeyHelper.MMKV_ACCESS_TOKEN
import www.wen.com.common.lib.helper.loading.KeyHelper.MMKV_OPEN_ID
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter
import www.wen.com.common.lib.util.SpUtil

/**
 * @author WEN
 * @Description:绑定手机号
 * @date 2020//25 17:48
 */
@AndroidEntryPoint
class BindPhoneFragment : BaseReceiveSMSFragment<FragmentBindPhoneBinding>(), View.OnClickListener{

    private val bindPhoneViewModel: BindPhoneViewModel by viewModels()

    private val proxyViewModel: ProxyViewModel by activityViewModels()

    private lateinit var mAccessToken:String

    private lateinit var mOpenId:String

    override val viewModel: BaseViewModel
        get() = bindPhoneViewModel

    override fun isEnableLoadingHelper() = true

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter = TitleBarUniversalAdapter(
        requireActivity(),
        action = ::titleAction
    )

    override fun getLayoutId(): Int = R.layout.fragment_bind_phone

    override fun getRecordTimeKey(): String = FORGET_REMAINDER_TIME

    override fun getSystemRecordTimeKey(): String = FORGET_SYSTEM_REMAINDER_TIME

    override fun lazyInitView() {
        super.lazyInitView()
        dataBinding.proxyViewModel = proxyViewModel
        dataBinding.bindPhoneViewModel = bindPhoneViewModel
        mAccessToken = requireArguments().getString(MMKV_ACCESS_TOKEN)!!
        mOpenId = requireArguments().getString(MMKV_OPEN_ID)!!
    }

    @FlowPreview
    override fun onClick(v: View) {
        when(v.id){
            R.id.btnLogin->{
                requestSmsLogin()
            }
            R.id.slVerificationCode->{
                sendVerificationCode(
                    dataBinding.tvPhoneNumber,
                    dataBinding.etVerificationCode,
                    proxyViewModel.itac.value!!,
                    bindPhoneViewModel
                )
            }
            R.id.tvCountryAreaCode->{
                showAreaPopupView()
            }
        }
    }

    /**
     * 绑定手机并跳转首页
     */
    @FlowPreview
    private fun requestSmsLogin() {
        bindPhoneViewModel.requestBindPhone(
                accessToken = mAccessToken,
                openId = mOpenId,
                smsCode = dataBinding.etVerificationCode.trimProxy(),
                mobile = dataBinding.tvPhoneNumber.trimProxy(),
        ) {
            loginInfo ->
            if (loginInfo.user.forbidStatus) {
                CreateXPopupHelper.createInformPopupView(PopupViewEntity().apply {
                    isShow = true
                    context = requireActivity()
                    title = getString(R.string.the_count_is_ban)
                    content = "因以下原因,用户「${loginInfo.user.nickName}」 已被封禁：\n\n【${loginInfo.user.forbidReason}】\n\n在 ${loginInfo.user.unForbidTime}之前，该账号不能进行登录"
                    confirmContent = getString(R.string.got_it)
                    confirmFunction = {
                        it.dismiss()
                    }
                })
            } else {
                SpUtil.encode(KeyHelper.MMKV_IS_FIRST_LOGIN,true)
                requireActivity().resetStartDestination(R.navigation.navigation, R.id.mainFragment)
            }
        }
    }

    private fun showAreaPopupView() {
        CreateXPopupHelper.createBottomAreaCodeListPopupView(PopupViewEntity().apply {
            this.context = requireActivity()
            isShow = true
            itac = proxyViewModel.itac.value!!
            areaCodeList = proxyViewModel.areaCodeEntities.value
            areaCodeListResultCallBack = {
                "+${it.code}".also { area ->
                    dataBinding.tvCountryAreaCode.text = area
                }
                proxyViewModel.itac.value = it.code
                SpUtil.encode(KeyHelper.MMKV_ITAC, it.code)
            }
        })
    }

}



